package com.wwwarehouse.contract.program_operation.resources_factor_page;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.contract.bean.ChooseModuleBean;
import com.wwwarehouse.contract.core.BaseViewPagerFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.CheckNeedResEvent;
import com.wwwarehouse.contract.event.NeedResourceEvent;
import com.wwwarehouse.contract.request.ModuleQueryRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/9/23.
 * 需要的资源要素viewpager
 */

public class NeedResourceFactorViewPagerFragment extends BaseViewPagerFragment implements View.OnClickListener {
	private List<ChooseModuleBean.ModuleList.BeanList> mNeedResourceFactorCheckedList = new ArrayList<>(); //拥有的资源要素已选的条目
	private List<ChooseModuleBean.ModuleList.BeanList> mCheckList = new ArrayList<>();
	private List<ChooseModuleBean.ModuleList.BeanList> mGangCaiDianDe = new ArrayList<>(); //拥有的资源要素已选的条目
	private boolean isFunction;

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		EventBus.getDefault().register(this);
		isFunction = getArguments().getBoolean(ContractConstant.KEY_IS_FUNCTION);
		mCustomScroll.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return true;
			}
		});
		mConfirmBtn.setVisibility(View.VISIBLE);
		mConfirmBtn.setOnClickListener(this);
	}

	@Override
	public void onResponse(String response, int what) {
		if (what == 0) {
			ChooseModuleBean chooseModuleBean = JSON.parseObject(response, ChooseModuleBean.class);
			if (chooseModuleBean != null && chooseModuleBean.getModuleList() != null && chooseModuleBean.getModuleList().getSize() > 0) {
				if (mNeedResourceFactorCheckedList.size() > 0) {
					for (int i = 0; i < mNeedResourceFactorCheckedList.size(); i++) {
						for (int j = 0; j < chooseModuleBean.getModuleList().getList().size(); j++) {
							if (mNeedResourceFactorCheckedList.get(i).getCode().equals(chooseModuleBean.getModuleList().getList().get(j).getCode())) {
								chooseModuleBean.getModuleList().getList().get(j).setSelect(mNeedResourceFactorCheckedList.get(i).isSelect());
							}
						}
					}
					mActivity.setTitle("需要采购的资源(" + mNeedResourceFactorCheckedList.size() + ")");
//					mActivity.setTitle("需要的资源要素");
					if (mNeedResourceFactorCheckedList.size() > 0) {
						mConfirmBtn.setEnabled(true);
						mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.blue_btn_circle));
					} else {
						mConfirmBtn.setEnabled(false);
						mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.btn_cir));
					}

				} else {

//					int selectCount = 0;
//					for (int i = 0; i < chooseModuleBean.getModuleList().getList().size(); i++) {
//						if (chooseModuleBean.getModuleList().getList().get(i).getSelected() == 1) {
//							mNeedResourceFactorCheckedList.add(chooseModuleBean.getModuleList().getList().get(i));
//							chooseModuleBean.getModuleList().getList().get(i).setSelect(true);
//							selectCount++;
//						}
//					}
//					if (selectCount > 0) {
//						mConfirmBtn.setEnabled(true);
//						mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.blue_btn_circle));
////						mActivity.setTitle("需要的资源要素(" + selectCount + ")");
//						mActivity.setTitle("需要的资源要素");
//					} else {
//						mConfirmBtn.setEnabled(false);
//						mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.btn_cir));
//					}

					if (isFunction) {
						mNeedResourceFactorCheckedList.clear();
						for (int i = 0; i < chooseModuleBean.getModuleList().getList().size(); i++) {
							if (chooseModuleBean.getModuleList().getList().get(i).getSelected() == 1) {
								chooseModuleBean.getModuleList().getList().get(i).setSelect(true);
								mNeedResourceFactorCheckedList.add(chooseModuleBean.getModuleList().getList().get(i));
							}
						}
						if (mNeedResourceFactorCheckedList.size() > 0) {
							mConfirmBtn.setEnabled(true);
							mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.blue_btn_circle));
							mActivity.setTitle("需要采购的资源(" + mNeedResourceFactorCheckedList.size() + ")");
//						mActivity.setTitle("需要的资源要素");
						} else {
							mConfirmBtn.setEnabled(false);
							mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.btn_cir));
						}
					}
				}
				setData(chooseModuleBean, (int) chooseModuleBean.getModuleList().getTotal(), 6, new NeedResourceFactorFragment().getClass().getName() , isFunction);

			}
		}
	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof NeedResourceFactorViewPagerFragment) {
			mActivity.setTitle("需要采购的资源");
		}
	}

	@Override
	public void requestDatas() {
		if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_NEED_RESOURCE_CHECKED_DATA) != null) {
			mNeedResourceFactorCheckedList = (List<ChooseModuleBean.ModuleList.BeanList>) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_NEED_RESOURCE_CHECKED_DATA);
		}
		if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_NEED_RESOURCE_PARAMS) != null) {
			ModuleQueryRequest mHaveResourceParams = (ModuleQueryRequest) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_NEED_RESOURCE_PARAMS);
			Map<String, Object> map = new HashMap<>();
			map.put("page", mHaveResourceParams.getBaseQuery().getPage());
			map.put("size", mHaveResourceParams.getBaseQuery().getSize());
			Map<String, Object> param = new HashMap<>();
			param.put("baseQuery", map);
//        param.put("buId", 260001);
			if (isFunction) {
				param.put("buId", mHaveResourceParams.getBuId());
			}
			param.put("sourceList", mHaveResourceParams.getSourceList());
			param.put("sourceModule", mHaveResourceParams.getSourceModule());
			param.put("taskType", mHaveResourceParams.getTaskType());
			sendRequest(ContractConstant.PLANNING_CHOOSE_MODULE, param, 0);

		}

	}

	public void onEventMainThread(NeedResourceEvent haveResourceEvent) {
		//step1  只添加手动选中的条目
		if (!mCheckList.contains(haveResourceEvent.getList()) && haveResourceEvent.getList().isSelect()) {
			mCheckList.add(haveResourceEvent.getList());
		} else if (mCheckList.contains(haveResourceEvent.getList()) && !haveResourceEvent.getList().isSelect()){
			mCheckList.remove(haveResourceEvent.getList());
		}

		//step 2 从两个List中删选， 如果把默认选中的条目手动取消了，则去默认选中的容器中删除，保证两个容器条目不重复。
		for (int i = 0; i < mNeedResourceFactorCheckedList.size(); i++) {
			if (!haveResourceEvent.getList().isSelect() && haveResourceEvent.getList().getCode().equals(mNeedResourceFactorCheckedList.get(i).getCode())) {
				mGangCaiDianDe.add(mNeedResourceFactorCheckedList.get(i));
				mNeedResourceFactorCheckedList.remove(i);
			}

		}
		//step 3 合并两个容器返回上层
		List<ChooseModuleBean.ModuleList.BeanList> finalList = new ArrayList<>();
		finalList.addAll(mCheckList);
		finalList.addAll(mNeedResourceFactorCheckedList);

		//
		if (finalList.size() > 0) {
			mActivity.setTitle("需要采购的资源(" + finalList.size() + ")");
//			mActivity.setTitle("需要的资源要素");
			mConfirmBtn.setEnabled(true);
			mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.blue_btn_circle));
		} else {
			mActivity.setTitle("需要采购的资源");
			mConfirmBtn.setEnabled(false);
			mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.btn_cir));
		}
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}

	@NonNull
	private List<ChooseModuleBean.ModuleList.BeanList> filterData() {
		List<ChooseModuleBean.ModuleList.BeanList> finalList = new ArrayList<>();
		finalList.addAll(mCheckList);
		finalList.addAll(mNeedResourceFactorCheckedList);
		return finalList;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.confirm_btn) {
			List<ChooseModuleBean.ModuleList.BeanList> finalList = filterData();
			EventBus.getDefault().post(new CheckNeedResEvent(finalList));
			popFragment();
		}
	}

	public void onEventMainThread(BackListenerEvent backListenerEvent) {
		if (backListenerEvent.getMsg().equals("NeedResourceFactorViewPagerFragment")) {
			mNeedResourceFactorCheckedList.addAll(mGangCaiDianDe);
			EventBus.getDefault().post(new CheckNeedResEvent(mNeedResourceFactorCheckedList));
			popFragment();
		}
	}
}